/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/
self.AnnotationViewer = function(container,annotations)
{
	this.annotations = annotations
	this.annotations_showing = new Object()
	
	this.el_main = Element("div",{className:"annotation_viewer"},container)
	this.el_canvas = Element("canvas",{className:"annotation_viewer_canvas"},this.el_main)
	this.el_canvas.width = $(this.el_canvas).width()
	this.el_canvas.height = $(this.el_canvas).height()
		
	this.canvas_ctx = this.el_canvas.getContext('2d');
	this.frame_rate = 2
	
	setInterval(bind(this,"displayTick"),(1000/this.frame_rate))
	$(document).bind("player_progress",bind(this,"onPlayerProgress"));
}
/**
update the annotation clowd every 10 fps
*/
AnnotationViewer.prototype.displayTick = function()
{	
	if(this.el_main.style.display != "none")
	{
		this.canvas_ctx.clearRect (0,0,this.el_canvas.width,this.el_canvas.height);
		var i = 0;
		for(var id in this.annotations_showing)
		{
			cv = this.annotations_showing[id]
			if(cv)
			{
				cv.step()
			}
		}
	}
}
/**
check whether we have to start displaying annotations within this second that are not already showing
only show when the movie in the playlist_item is the current movie
*/
AnnotationViewer.prototype.onPlayerProgress = function(evt,playlist_item,time_in_movie,time_in_fragment)
{
	this.time = time_in_movie
	
	for(var i=0;i<this.annotations.length;i++)
	{
		var annotation = this.annotations[i]
		var key = ""+annotation.id
		
		show = false
		
		if(playlist_item.movie == annotation.movie )
		{
			begin = parseInt(annotation.getBeginTime())
			end = parseInt(annotation.getEndTime())
			
			//console.log("begin:" + begin + " end:" + end + " now:" +time_in_movie)
			
			if((time_in_movie >= begin) && (time_in_movie <= end) ) //annotation should be showing
			{
				show = true		
				if(this.annotations_showing[key] == null)
				{
					steps = this.frame_rate * annotation.duration
					steps_done = (time_in_movie - begin) * this.frame_rate //incase we start in the middle of it
					var canvas = new AnnotationCanvasItem(this.annotations[i],this.canvas_ctx,steps,steps_done)	
					this.annotations_showing[key] = canvas					
				}
			}
		}
		if(!show)
		{
		
			if(this.annotations_showing[key] != null)
			{
				this.annotations_showing[key] = null
			}
		}		
	}
}
/**
holds data for the animtation of one annotation on the canvas
*/
self.AnnotationCanvasItem = function(annotation,ctx,steps,steps_done)
{
	this.annotation = annotation;
	this.ctx = ctx
	this.steps = steps;
	this.steps_done = steps_done
	this.text = this.annotation.toText(20)
}
AnnotationCanvasItem.prototype.step = function()
{
	if(this.steps_done < this.steps)
	{
		p = this.steps_done / this.steps
	
		min_font = 10
		max_font = 30
		dif_font = max_font - min_font
		size = 0
		if(p > 0.5)
		{
			size = (2 * (1 - p) * dif_font) + min_font	
		}
		else
		{
			size = (p * 2 * dif_font) + min_font
		}
		
		this.ctx.fillStyle    = "rgba(200,200,200,0.8)";
		this.ctx.font         = size + 'pt Arial';
		this.ctx.textBaseline = 'top';
		dim = this.ctx.measureText(this.text)
		tw = dim.width
		
		pos = ((this.ctx.canvas.width + tw) * p) - tw
	
		this.ctx.fillText  (this.text, pos, 10); //
		this.steps_done++
	}
}

